app 在呼叫 api 時,api 會要求填一些欄位 app name 、package name、app version等資訊,
package_info_plus 就可以取得app的資訊
可以透過PackageInfo查詢應用程式包的資訊。
PackageInfo packageInfo = await PackageInfo.fromPlatform();
String appName = packageInfo.appName;
String packageName = packageInfo.packageName;
String version = packageInfo.version;
String buildNumber = packageInfo.buildNumber;
直接在 pubspec.yaml 加上 package_info_plus: ^4.1.0 ,然後pub get
dependencies:
package_info_plus: ^4.1.0
在 /lib/main.dart 加入 程式
import 'package:package_info_plus/package_info_plus.dart';
宣告變數和初始化
class _MyHomePageState extends State<MyHomePage> {
PackageInfo _packageInfo = PackageInfo(
appName: 'Unknown',
packageName: 'Unknown',
version: 'Unknown',
buildNumber: 'Unknown',
buildSignature: 'Unknown',
installerStore: 'Unknown',
);
@override
void initState() {
super.initState();
_initPackageInfo();
}
Future<void> _initPackageInfo() async {
final info = await PackageInfo.fromPlatform();
setState(() {
_packageInfo = info;
});
}
Widget _infoTile(String title, String subtitle) {
return ListTile(
title: Text(title),
subtitle: Text(subtitle.isEmpty ? 'Not set' : subtitle),
);
}
infoTile('App name', _packageInfo.appName),
_infoTile('Package name', _packageInfo.packageName),
_infoTile('App version', _packageInfo.version),
_infoTile('Build number', _packageInfo.buildNumber),
_infoTile('Build signature', _packageInfo.buildSignature),
是不是很簡單就可以用程式去讀取app 的資訊呀 !!